/*
 * @: 异步串行钩子
 * @Version: 1.0
 * @Autor: ziwei
 * @Date: 2021-06-28 09:26:41
 * @LastEditors: ziwei
 * @LastEditTime: 2022-02-11 14:26:16
 */
const { AsyncSeriesHook } = require('tapable');
const hook = new AsyncSeriesHook(['name', 'age']);

//异步注册 全局任务完成后执行回调 Promise.all
console.time('cost');
/* 
hook.tapAsync("1", (name, age,callback) => {
setTimeout(() => {
    console.log(1,name,age);
    callback();
}, 1000);
})
hook.tapAsync("1", (name, age,callback) => {
setTimeout(() => {
    console.log(1,name,age);
    callback();
}, 2000);
})
hook.tapAsync("1", (name, age,callback) => {
setTimeout(() => {
    console.log(1,name,age);
    callback();
}, 3000);
})

hook.callAsync("xiayu",12,(err)=>{
  console.log('err',err);
  console.timeEnd('cost');
}) 
*/

hook.tapPromise('1', (name, age, callback) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, 1000);
  });
});
hook.tapPromise('1', (name, age, callback) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, 2000);
  });
});
hook.tapPromise('1', (name, age, callback) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, 3000);
  });
});

hook.promise('xiayu').then((result) => {
  console.log(result);
  console.timeEnd('cost');
});
